package entidades;

import util.MySQL;
import java.util.List;
import java.util.HashMap;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.google.gson.Gson;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 *
 * @author JFM
 */
public class Estacionalidad extends Entidad
	{
	private int newlongId;
	private float indice;
	private int mes;

	private Newlong newlong;

	static final String NOMBRE_TABLA = "estacionalidades";



	// --- Constructores --- //

	public Estacionalidad()
		{
		id = 0;
		mes = 1;
		indice = 1.0f;

		newlongId = 0;
		newlong = null;
		}

	public Estacionalidad(int id) throws SQLException
		{ super(id); }

	public Estacionalidad(String id) throws SQLException
		{ super(id); }

	public Estacionalidad(ResultSet result) throws SQLException
		{ super(result); }



	// --- Metodos get()/set() --- //

	public int getMes()
		{ return mes; }
	public void setMes(int mes)
		{ this.mes = mes; }
	public void setMes(String mes)
		{ try{ this.mes = Integer.parseInt(mes); }catch(Exception e) {}; }

	public float getIndice()
		{ return indice; }
	public void setIndice(float indice)
		{ this.indice = indice; }
	public void setIndice(String indice)
		{ try{ this.indice = Float.parseFloat(indice); }catch(Exception e) {}; }

	public Newlong getNewlong() throws SQLException
		{
		if(newlong == null || newlongId != newlong.getId())
			{ newlong = new Newlong(newlongId); }
		return newlong;
		}

	public int getNewlongId()
		{ return newlongId; }
	public void setNewlongId(int newlongId)
		{ this.newlongId = newlongId; }
	public void setNewlongId(String newlongId)
		{ try{ this.newlongId = Integer.parseInt(newlongId); }catch(Exception e) {}; }

	@Override
	public String getDescriptor()
		{ return "Newlong " + newlongId + ": " + mes + " - " + indice; }

	@Override
	public String toJson()
		{
		HashMap map = new HashMap();
		map.put("id", id);
		map.put("newlong_id", newlongId);
		map.put("mes", mes);
		map.put("indice", indice);
		return new Gson().toJson(map);
		}



	// --- Metodos MySQL --- //

	@Override
	public String getNombreTabla()
		{ return NOMBRE_TABLA; }

	@Override
	public void leer(ResultSet result, String prefijo) throws SQLException
		{
		setId(result.getInt(prefijo + "id"));
		setMes(result.getInt(prefijo + "mes"));
		setIndice(result.getFloat(prefijo + "indice"));
		setNewlongId(result.getInt(prefijo + "newlong_id"));
		}

	@Override
	public void guardar() throws SQLException
		{
		ResultSet result = MySQL.executeQuery("Call Estacionalidades_Save(" +
				MySQL.formatId(id) + "," +
				MySQL.formatId(newlongId) + "," +
				MySQL.format(mes) + "," +
				MySQL.format(indice) + ")");

		if(result.next()) { leer(result); }

		result.getStatement().close();
		}

	public static void guardar(int newlongId, int mes, float indice) throws SQLException
		{
		ResultSet result = MySQL.executeQuery("Call Estacionalidades_Save(" +
				"null," +
				MySQL.formatId(newlongId) + "," +
				MySQL.format(mes) + "," +
				MySQL.format(indice) + ")");

		result.getStatement().close();
		}

	public static void borrar(String id) throws SQLException
		{ Entidad.borrar(id, NOMBRE_TABLA); }

	public static List<Estacionalidad> listar() throws SQLException
		{ return (List) Entidad.listar(Estacionalidad.class, NOMBRE_TABLA, "newlong_id ASC, mes ASC"); }

	public static float[] getIndices(Newlong newlong) throws SQLException
		{ return getIndices(newlong.getId()); }

	public static float[] getIndices(int newlongId) throws SQLException
		{
		float[] res = new float[12];
		for(int i=0; i<res.length; i++) {res[i] = 1.0f;}

		ResultSet result = MySQL.executeQuery("SELECT * FROM " + NOMBRE_TABLA + " WHERE newlong_id = " + newlongId);
		while(result.next())
			{ res[result.getInt("mes") - 1] = result.getFloat("indice"); }
		result.getStatement().close();
		
		return res;
		}

	public static int getDiasCiclo(int newlongId, Date fecha) throws SQLException
		{
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(fecha);

		int dias = (int)( getIndices(newlongId)[gc.get(Calendar.MONTH)] * Tiempos.getCicloMedio() );
		if(dias < Tiempos.getCicloMinimo()) dias = Tiempos.getCicloMinimo();
		if(dias > Tiempos.getCicloMaximo()) dias = Tiempos.getCicloMaximo();

		return dias;
		}
	}
